<?php

namespace Home\Controller;

/**
* 
*/
class AccountController extends HomeController
{
	/**
	 * 提现
	 * @author qlj
	 */
	public function cash(){
		if (IS_POST) {
			// $res = D('CourseRecord') -> is_complete(session('uid'));
			// if (!empty($res)) {
			// 	$this -> error('您还有尚未完成的课程，请完成所有课程后再尝试提现');
			// 	return;
			// }

			if ($_POST['change'] == '') {
				$this -> error('请填写金额');
				return;
			}
			if (!is_numeric($_POST['change'])) {
				$this -> error('请输入数字');
				return;
			}
			if ($_POST['change'] > $_POST['account']) {
				$this -> error('提现金额不能大于余额');
				return;
			}
			if ($_POST['change']  == 0 ||$_POST['change']  == 0.0 || $_POST['change']  == 0.00) {
				$this -> error('无法提现'.$_POST['change'].'元');
				return;
			}
			$_POST['uid']		  = session('uid');
			$_POST['create_time'] = time();
			$_POST['action']	  = 3;
			$_POST['obj']		  = session('uid');			//==			$_POST['openid']	= session('openid');

			$data['id'] 	 = $_POST['uid'];
			$data['account'] = floatval($_POST['account']) - floatval($_POST['change']);			
			//开启事务
			$model = M();
			$model -> startTrans();
			//判断是否执行成功
			if (D('Account') -> addAccount($_POST) && D('UserMember') -> updatePost($data)) {
				//提交事务
				$model -> commit();
				$this -> success('您提现的金额将在2日内提至您的微信钱包。');
			} else {
				//回滚
				$model -> rollback();
				$this -> error('出错了');
			}
		} else {
			//查找用户数据
			$data 	 = D('UserMember') -> showUser(session('uid'));	
			/* $account = $data['account']; */						/* yxf 20171107 金额有几位小数就显示几位  */						$account = floatval($data['account']);				
			$this -> assign('account',$account);
			$this -> display();
		}
	}

	/**
	 * 流水明细
	 */
	public function detailed(){
		$detailed = D('Account') -> account(session('uid'));				/* yxf 20171107 金额有几位小数就显示几位  S*/				foreach($detailed as &$v){						$v['change'] =  floatval($v['change']);					}				unset($v);				/* yxf 20171107 金额有几位小数就显示几位  E*/				
		$this -> assign('detailed', $detailed);
		$this -> display();
	}

	/**
	 * 会员充值
	 * @author qlj
	 */
	public function recharge(){
			if(IS_POST){
				$guest['order'] = time();
				$guest['price'] = $_POST['price'];
				$guest['uid']   = session('uid');
				$guest['become_time'] = $_POST['money'] * 24 * 3600;
				$guest['create_time'] =time();
				$data = D('Guest') ->addGuest($guest);
				// dump($data);die;
				$return =D('Guest')->where(array('id'=>$data))->find();
				// $out=$return['order'];
				 $Columns=A("Wxpay");
   				 $ree=$Columns->weixinpay_jsss($out);//支付完成
			}					
			if($_GET['guest'] > 0){
			//开启事务
			$order = $_GET['guest'];
			$model = M();
			$model -> startTrans();
			$result = D('Guest') ->where(array('order' =>$order))->find();
			//添加平台流水
			$data['uid'] 		 = session('uid');
			$data['change'] 	 = $result['price'];
			$data['action']		 = 2;
			$data['obj']		 = 0;
			$data['create_time'] = time();
			$res0 = D('Account') -> addAccount($data);
			$return = D('Guest') ->where(array('order' =>$_GET['guest'])) ->find();
 			//查出用户数据
            $user  = D('UserMember') ->where(array('id'=>$return['uid']))->find();
           if ($user['vip_endtime'] < time()) {
                //到期日为当前时间加上购买的时间
                $endtime = $return['become_time'] + time();
            } else {
                //到期日为会员结束时间加上购买的时间
                $endtime = intval($user['vip_endtime']) + $return['become_time'];
            }			

            //更新会员到期日
            $update['id'] = $return['uid'];
            $update['vip_endtime'] = $endtime;
            $res1 = D('UserMember') -> where(array('id' =>$update['id'])) ->save(array('vip_endtime' =>$update['vip_endtime']));			//yyy 同步更新会员到期日			$endtime = D('Guest') -> where(array('uid' =>$update['id'],'order' =>$_GET['guest'])) ->save(array('end_time' =>$update['vip_endtime']));

			//发送系统信息
			$msg['sid'] = 0;
			$msg['rid'] = session('uid');
			$msg['content'] = "您开通会员成功";
			$msg['type'] = 4;
			$res2 = D('Admin/Message') -> addMessage($msg);

			//判断事务是否执行
			if ($res0 && $res2) {
				//提交
				$model -> commit();
				$this -> success('充值成功',U('index/index'));die;
			} else {
				//回滚
				$model -> rollback();
				$this -> error('出错了，请稍后再试');
			}
		}
			//当前用户id
			$id   = session('uid');
			//用户信息
			$data = D('Account') ->userInfo($id);			/* lkl新增获取用户信息 S*/			$userinfo  = D('UserMember') ->where(array('id'=>$id))->find();	
			$this ->assign('userinfo',$userinfo);			/* lkl新增获取用户信息 E*/			$this ->assign('data',$data);
			//判断是否是会员
			$vip  = is_vip($id);			
			$this ->assign('vip',$vip);
			$this -> display();

	}
}